package com.mall.mymall.controller;

import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.querypage.AddressPageRequest;
import com.mall.mymall.dao.entity.Addresses;
import com.mall.mymall.service.AddressesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/addresses")
public class AddressesController {
    @Autowired
    AddressesService addressesService;

    @PostMapping("/pageMyAddresses")
    public Result<QueryDto<Addresses>> pageMyAddresses(@RequestBody AddressPageRequest addressPageRequest) {
        QueryDto<Addresses> addressesQueryDto = addressesService.pageAddresses(addressPageRequest);
        return Result.success(addressesQueryDto);
    }

    @PostMapping("/add")
    public Result<Addresses> addAddress(@RequestBody Addresses addresses) {
        addressesService.save(addresses);
        return Result.success(addresses);
    }

    @PutMapping("/update")
    public Result<Addresses> updateAddress(@RequestBody Addresses addresses) {
        addressesService.updateById(addresses);
        return Result.success(addresses);
    }

    @DeleteMapping("/delete/{addressId}")
    public Result<String> deleteAddress(@PathVariable Integer addressId) {
        addressesService.removeById(addressId);
        return Result.success("删除成功");
    }
}
